<?php
/**
 * Created by PhpStorm.
 * User: HCDX
 * Date: 2018/5/25
 * Time: 17:40
 */
namespace app\admin\model;
use think\Model;

class Admin extends Model
{
    public function addAdmin($data){
        if(empty($data)|| !is_array($data)){
            return false;
        }
        if($data['password']){
            $data['password']=md5($data['password']);
        }
        $adminData = array();
        $adminData['name'] = $data['name'];
        $adminData['phone'] = $data['phone'];
        $adminData['email'] = $data['email'];
        $adminData['password'] = $data['password'];
        if($this->save($adminData)){
            $groupAccess['uid'] =  $this->id;
            $groupAccess['group_id'] = $data['group_id'];
            db('auth_group_access')->insert($groupAccess);
            return true;
        }else{
            return false;
        }
    }
    public function login($data){
        $admin = Admin::getByName($data['name']);
        if($admin){
            if($admin['password']==md5($data['password'])){
                session('id',$admin['id']);
                session('name',$admin['name']);
                return 2;//登录密码正确
            }else{
                return 3;//登录密码不正确
            }
        }else{
            return 1;//用户不存在
        }
    }
}